home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / PGUIDE / PROGWOB / PWOEVENT.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-11-26  |  5.4 KB  |  170 lines

  1. VERSION 5.00
  2. Begin VB.Form frmEvents 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Raising and Handling Events"
  5.    ClientHeight    =   4245
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   4710
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   4245
  13.    ScaleWidth      =   4710
  14.    StartUpPosition =   3  'Windows Default
  15.    Begin VB.CommandButton cmdAddForm 
  16.       Caption         =   "Add &Receiver"
  17.       Height          =   375
  18.       Left            =   2880
  19.       TabIndex        =   3
  20.       Top             =   3360
  21.       Width           =   1695
  22.    End
  23.    Begin VB.TextBox txtMessage 
  24.       Height          =   285
  25.       Left            =   120
  26.       TabIndex        =   2
  27.       Top             =   3000
  28.       Width           =   4455
  29.    End
  30.    Begin VB.CommandButton cmdPercentDone 
  31.       Caption         =   "&Start a long task that uses an event to report progress"
  32.       Height          =   615
  33.       Left            =   240
  34.       TabIndex        =   0
  35.       Top             =   120
  36.       Width           =   4215
  37.    End
  38.    Begin VB.Label Label2 
  39.       Caption         =   "Message after all recipients have handled it:"
  40.       Height          =   375
  41.       Left            =   120
  42.       TabIndex        =   6
  43.       Top             =   3360
  44.       Width           =   2295
  45.    End
  46.    Begin VB.Label lblPercentDone 
  47.       Height          =   255
  48.       Left            =   360
  49.       TabIndex        =   5
  50.       Top             =   960
  51.       Width           =   4095
  52.    End
  53.    Begin VB.Label lblEcho 
  54.       Height          =   255
  55.       Left            =   120
  56.       TabIndex        =   4
  57.       Top             =   3840
  58.       Width           =   4455
  59.    End
  60.    Begin VB.Label Label1 
  61.       Caption         =   $"PWOEvent.frx":0000
  62.       Height          =   1335
  63.       Left            =   120
  64.       TabIndex        =   1
  65.       Top             =   1560
  66.       Width           =   4455
  67.    End
  68.    Begin VB.Line Line1 
  69.       X1              =   120
  70.       X2              =   4560
  71.       Y1              =   1440
  72.       Y2              =   1440
  73.    End
  74. Attribute VB_Name = "frmEvents"
  75. Attribute VB_GlobalNameSpace = False
  76. Attribute VB_Creatable = False
  77. Attribute VB_PredeclaredId = True
  78. Attribute VB_Exposed = False
  79. Option Explicit
  80. ' ========================================
  81. '     Declarations for Broadcast Demo
  82. ' The Broadcast event has one argument,
  83. '   the message to be sent.  The argument
  84. '   is ByRef, so recipients can change it.
  85. Event Broadcast(Message As String)
  86. ' Collection of receivers.
  87. Private mcolReceivers As New Collection
  88. ' ========================================
  89. '       Declarations and Code for
  90. '           Percent Done Demo
  91. Private WithEvents mWidget As Widget
  92. Attribute mWidget.VB_VarHelpID = -1
  93. Private mblnCancel As Boolean
  94. Private Sub mWidget_PercentDone(ByVal Percent As Double, Cancel As Boolean)
  95.     lblPercentDone.Caption = CInt(100 * Percent) & " percent complete"
  96.     DoEvents
  97.     If mblnCancel Then Cancel = True
  98. End Sub
  99. Private Sub cmdPercentDone_Click()
  100.     Static blnProcessing As Boolean
  101.     If blnProcessing Then
  102.         mblnCancel = True
  103.     Else
  104.         blnProcessing = True
  105.         cmdPercentDone.Caption = "&Cancel Task"
  106.         mblnCancel = False
  107.         lblPercentDone.Caption = "0 percent complete"
  108.         lblPercentDone.Refresh
  109.         
  110.         ' Create a Widget and start the
  111.         '   long-running task.
  112.         Set mWidget = New Widget
  113.         On Error Resume Next
  114.         Call mWidget.LongTask(14.4, 0.9)
  115.         '
  116.         ' See if the call ended because it
  117.         '   was canceled (can't just test
  118.         '   mblnCancel for this, because
  119.         '   it might have been set just as
  120.         '   LongTask was returning).
  121.         If Err.Number = 0 Then
  122.             lblPercentDone.Caption = "Task Complete"
  123.         ElseIf Err.Number = vbObjectError + wdgERRTaskCanceled Then
  124.             lblPercentDone.Caption = "Task Canceled"
  125.         Else
  126.             ' (Handling for other errors omitted.)
  127.             lblPercentDone.Caption = "Something bad happened"
  128.         End If
  129.         Set mWidget = Nothing
  130.         cmdPercentDone.Caption = "&Start a long task that uses an event to report progress"
  131.         blnProcessing = False
  132.     End If
  133. End Sub
  134. ' ========================================
  135. '        Code for Broadcast Demo
  136. Private Sub cmdAddForm_Click()
  137.     Dim frm As New frmReceiver
  138.     ' Keep track of the receivers.
  139.     mcolReceivers.Add frm
  140.     frm.Show vbModeless, Me
  141.     Me.SetFocus
  142.     txtMessage.SetFocus
  143. End Sub
  144. Private Sub Form_Unload(Cancel As Integer)
  145.     Dim frm As frmReceiver
  146.     On Error Resume Next
  147.     Do While mcolReceivers.Count > 0
  148.         Unload mcolReceivers(1)
  149.         mcolReceivers.Remove 1
  150.     Loop
  151. End Sub
  152. Private Sub txtMessage_Change()
  153.     Dim strMessage As String
  154.     strMessage = txtMessage.Text
  155.     '
  156.     ' Raise the Broadcast event.  Note that
  157.     '   there's no way of knowing if there
  158.     '   are any receivers handling the
  159.     '   event.
  160.     RaiseEvent Broadcast(strMessage)
  161.     '
  162.     ' Display the message after all
  163.     '   receivers (if any) have handled
  164.     '   it.  Note that there's no way
  165.     '   to know which receiver altered the
  166.     '   message, or what interim values
  167.     '   the message may have had.
  168.     lblEcho = strMessage
  169. End Sub
  170.